package com.qf.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.qf.model.entity.Manager;
import com.qf.service.ManagerService;

import java.util.ArrayList;
import java.util.List;

/**
 * --- 天道酬勤 ---
 *
 * @author TRC
 * @date 2024/4/30
 * @desc
 */
public class ManagerListener extends AnalysisEventListener<Manager> {

    private List<Manager> managerList = new ArrayList<>();

    private static final int BATCH_COUNT = 5;

    private ManagerService managerService;

    public ManagerListener() {
    }

    public ManagerListener(ManagerService managerService) {
        this.managerService = managerService;
    }

    @Override
    public void invoke(Manager manager, AnalysisContext analysisContext) {
        managerList.add(manager);
        if (managerList.size() >= BATCH_COUNT) {
            managerService.insertBatch(managerList);
            managerList.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (managerList.size() > 0) {
            managerService.insertBatch(managerList);
            managerList.clear();
        }
    }
}
